//
//  WorkingTableViewController.m
//  EasyGo
//
//  Created by newhuidong on 16/1/22.
//  Copyright © 2016年 HuiDong. All rights reserved.
//

#import "WorkingTableViewController.h"
#import "MyProfileViewController.h"
#import "MyMeansViewController.h"
#import "HomeViewController.h"
#import "MBProgressHUD+NJ.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"
#import "AppDelegate.h"
#import "URL.h"

#import "MJRefresh.h"
@interface WorkingTableViewController (){
    
    NSInteger  _pageindex;
    
    UIImageView *navBarHairlineImageView;

}
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *name_label;

@property (weak, nonatomic) IBOutlet UILabel *phone_label;
@property (weak, nonatomic) IBOutlet UILabel *grades_label;
@end

@implementation WorkingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.headImage.layer.cornerRadius=45;
    self.headImage.layer.masksToBounds=YES;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:0/255.0f green:128/255.0f blue:255/255.0f alpha:1];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //self.navigationController.navigationBar.shadowImage=[[UIImage alloc]init];
    
    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    [self  setupRefresh];
}
#pragma 刷新
-(void)setupRefresh{
    
    MJRefreshNormalHeader * header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshData];
    }];
    [header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
    [header beginRefreshing];
    self.tableView.mj_header=header;
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _pageindex ++;
        [self loadData];
        
    }];
}
-(void)refreshData{
    
    _pageindex =1;
    [self loadData];
    
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    navBarHairlineImageView.hidden = NO;
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    navBarHairlineImageView.hidden = YES;
    
    [MBProgressHUD hideHUD];
    [self loadData];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 0.1;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        HomeViewController * my=[[HomeViewController alloc]init];
        
        [self.navigationController pushViewController:my
                                             animated:YES];
        
    }
    
}


-(void)loadData{
    
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    manager.responseSerializer=[AFJSONResponseSerializer serializer];
    
    AppDelegate * temp=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    
    NSMutableDictionary * paramer=[[NSMutableDictionary alloc]init];
    
    paramer[@"token"]=temp.token;
    
    
    NSString *urlString=[NSString stringWithFormat:@"%@%@",url1,@"GetMyMsg"];
    
    
    [manager POST:urlString parameters:paramer progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic=(NSDictionary*)responseObject;
        
        NSString * string=dic[@"Status"];
        
        int intString=[string intValue];
        
        if (intString==0) {
            
            self.title=@"下班";
        }else if (intString==1){
            
            self.title=@"上班中";
        }
            
            
        NSDictionary *dict=dic[@"Data"];
        
        self.name_label.text=[dict objectForKey:@"Name"];
        self.phone_label.text=[dict objectForKey:@"PhoneNum"];
        self.grades_label.text=[NSString stringWithFormat:@"%@",[dict objectForKey:@"Score"]];
        [self.tableView.mj_header endRefreshing];
        [self.tableView .mj_footer endRefreshing];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"Error:%@",error);
        
    }];
    
}
#pragma 隐藏导航栏下面的一条线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
@end
